<template>
  <div>
    <el-dialog
      :title="ruleForm.id == '' ? '添加鱼池' : '编辑鱼池'"
      :visible.sync="addEditIsDialog_c"
      @close="resetForm('formName')"
      width="800px"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="formName"
        label-width="120px"
        class="demo-ruleForm"
        :class="{ 'form-rules-show': seeType }"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司名称：" prop="companyId">
              <el-cascader
                v-model="ruleForm.companyId"
                placeholder="请选择公司名称"
                :options="companyArray"
                :props="{
                  checkStrictly: true,
                  value: 'id',
                  label: 'name',
                  children: 'childList',
                }"
                @change="changeTreeItem"
                clearable
                :disabled="seeType"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="基地名称：" prop="baseId">
              <el-select
                v-model="ruleForm.baseId"
                placeholder="请选择基地名称"
                clearable
              >
                <el-option
                  v-for="item in baseArray"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="部门编号：" prop="deptNum">
              <el-input
                v-model="ruleForm.deptNum"
                placeholder="请输入部门编号"
                clearable
                maxlength="50"
                show-word-limit
                :disabled="seeType"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="部门班组：" prop="deptTeam">
              <el-input
                v-model="ruleForm.deptTeam"
                placeholder="请输入部门班组"
                clearable
                maxlength="50"
                show-word-limit
                :disabled="seeType"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="班组名称：" prop="deptTeamName">
              <el-input
                v-model="ruleForm.deptTeamName"
                placeholder="请输入班组名称"
                clearable
                maxlength="50"
                show-word-limit
                :disabled="seeType"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="鱼池编号：" prop="fishpondCode">
              <el-input
                v-model="ruleForm.fishpondCode"
                placeholder="请输入鱼池编号"
                clearable
                maxlength="50"
                show-word-limit
                :disabled="seeType"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="鱼池名称：" prop="fishpondName">
              <el-input
                v-model="ruleForm.fishpondName"
                placeholder="请输入鱼池名称"
                clearable
                maxlength="50"
                show-word-limit
                :disabled="seeType"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="鱼池容积：" prop="size">
              <el-input-number
                v-model="ruleForm.size"
                placeholder="请输入鱼池容积"
                :precision="2"
                :step="1"
                :max="999999"
                :min="0"
                :disabled="seeType"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排：" prop="row">
              <el-input
                v-model="ruleForm.row"
                placeholder="请输入排"
                clearable
                maxlength="50"
                show-word-limit
                :disabled="seeType"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="鱼类名称：" prop="fishTypeId">
              <el-select
                v-model="ruleForm.fishTypeId"
                placeholder="请选择鱼类名称"
                clearable
              >
                <el-option
                  v-for="item in fishTypeArray"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="鱼数量：" prop="fishNum">
              <el-input-number
                v-model="ruleForm.fishNum"
                placeholder="请输入鱼数量"
                :precision="0"
                :step="1"
                :max="9999"
                :min="0"
                :disabled="seeType"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="卵径：" prop="fishEggSize">
              <el-input-number
                v-model="ruleForm.fishEggSize"
                placeholder="请输入卵径"
                :precision="2"
                :step="1"
                :max="9999"
                :min="0"
                :disabled="seeType"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="鱼类初重：" prop="fishInitWeight">
              <el-input-number
                v-model="ruleForm.fishInitWeight"
                placeholder="请输入鱼类初重"
                :precision="2"
                :step="1"
                :max="9999"
                :min="0"
                :disabled="seeType"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="鱼类当重：" prop="fishNowWeight">
              <el-input-number
                v-model="ruleForm.fishNowWeight"
                placeholder="请输入鱼类当重"
                :precision="2"
                :step="1"
                :max="9999"
                :min="0"
                :disabled="seeType"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button class="btn-light-blue" @click="submitForm('formName')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { getObjFishpond,saveObjFishpond } from "@/api/fishpond";
import { getCompanyInfoTree } from "@/api/company";
import { getSysCompanyBases, getSysFishTypes} from "@/api/system";

export default {
  components: {},
  props: {
    addEditId: {
      type: String,
      default: "",
    },
    addEditStatus: {
      type: String,
      default: "add",
    },
    addEditIsDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: "",
        companyId: "",
        baseId: "",
        deptNum: "",
        deptTeam: "",
        deptTeamName: "",
        fishpondCode: "",
        fishpondName: "",
        size: "",
        row: "",
        fishTypeId: "",
        fishNum: "",
        fishEggSize: "",
        fishInitWeight: "",
        fishNowWeight: "",
      },
      rules: {
        // 表单规则
        companyId: [
          { required: true, message: "请选择公司名称", trigger: "change" },
        ],
        baseId: [
          { required: true, message: "请选择基地名称", trigger: "change" },
        ],
        deptNum: [{ required: true, message: "请输入部门编号", trigger: "blur" }],
        deptTeam: [{ required: true, message: "请输入部门班组", trigger: "blur" }],
        deptTeamName: [{ required: true, message: "请输入部门名称", trigger: "blur" }],
        fishpondCode: [{ required: true, message: "请输入鱼池编号", trigger: "blur" }],
        fishpondName: [{ required: true, message: "请输入鱼池名称", trigger: "blur" }],
        size: [{ required: true, message: "请输入鱼池容积", trigger: "blur" }],
        row: [{ required: true, message: "请输入排", trigger: "blur" }],
        fishTypeId: [{ required: true, message: "请输入鱼类名称", trigger: "blur" }],
        fishNum: [{ required: true, message: "请输入鱼数量", trigger: "blur" }],
        fishEggSize: [{ required: true, message: "请输入卵径", trigger: "blur" }],
        fishInitWeight: [{ required: true, message: "请输入鱼类初重", trigger: "blur" }],
        fishNowWeight: [{ required: true, message: "请输入鱼类当重", trigger: "blur" }],
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      baseArray: [], // 基地集合
      companyArray: [], // 公司集合
      fishTypeArray: [], // 鱼类集合
      userType: "", // 用户类型（0超级管理员，1公司管理员）
    };
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val;
      if (this.addEditIsDialog_c) {
        // 添加
        if (this.addEditStatus == "add") {
          this.ruleForm.id = "";
          this.seeType = false;
        }

        // 编辑
        if (this.addEditStatus == "edit") {
          this.ruleForm.id = this.addEditId;
          this.seeType = false;
          this.getDetail();
        }

        // 查看
        if (this.addEditStatus == "see") {
          this.ruleForm.id = this.addEditId;
          this.seeType = true;
          this.getDetail();
        }

        this.userType = Number(window.localStorage.getItem("userType"));
      }
    },
  },
  methods: {
    // 详情
    getDetail() {
      getObjFishpond({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          this.ruleForm = res.data;

          // 获取公司列表
          this.getCompanyInfoTree();

          this.getSysCompanyBases();

          this.getSysFishTypes();
        }
      });
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveObjFishpond(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
        }
      });
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$parent.closeAddEdit();
    },
    // 获取公司列表
    getCompanyInfoTree() {
      if (this.ruleForm.userType === "") {
        return;
      }

      if (this.ruleForm.userType == 0) {
        // 超级管理员，公司只有一个总公司
        this.companyArray = [
          {
            id: 0,
            name: "总公司",
            parentId: 0,
          },
        ];
      } else {
        getCompanyInfoTree({}).then((res) => {
          if (res.code == 200) {
            this.companyArray = res.data;
          }
        });
      }
    },
    // 公司下拉切换
    changeTreeItem(val) {
      if (val) {
        this.ruleForm.companyId = val[val.length - 1];
      } else {
        this.ruleForm.companyId = "";
      }

      this.ruleForm.baseId = "";
      this.baseArray = [];

      // 获取基地列表
      this.getSysCompanyBases();
    },
    // 获取基地列表
    getSysCompanyBases() {
      if (this.ruleForm.companyId === "" || this.ruleForm.companyId == null) {
        return;
      }

      getSysCompanyBases({ id: this.ruleForm.companyId }).then((res) => {
        if (res.code == 200) {
          this.baseArray = res.data;
        }
      });
    },
    getSysFishTypes() {
      getSysFishTypes().then((res) => {
        if (res.code == 200) {
          this.fishTypeArray = res.data;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;
}

.el-cascader {
  width: 100%;
}

.el-select {
  width: 100%;
}
</style>
